package org.javagril.yuetong.service;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.javagril.yuetong.bean.RespBean;
import org.javagril.yuetong.bean.RespPageBean;
import org.javagril.yuetong.bean.Scheduling;
import org.javagril.yuetong.bean.Truck;
import org.javagril.yuetong.mapper.TruckMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

/**
 * @author QingtungTree
 * @Datetime: 2019/12/23
 * @Description:org.javagril.yuetong.service
 * @Version:
 */
@Service
public class TruckService {
    @Autowired
    TruckMapper truckMapper; /*注入接口*/

    /*分页查询的数据*/
    public RespPageBean getAllTrucksByPage(Integer page, Integer size) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Truck> list = truckMapper.getAllTrucksByPage(page, size);
        long total = truckMapper.getTotal();
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setTotal(total);
        respPageBean.setData(list);

        return respPageBean;
    }

    /*添加方法*/
    public Integer addTruck(Truck truck) {
        truck.setAltertime(new Date());
        truck.setIsdelete(-1);
        truck.setCheckintime(new Date());

        return truckMapper.insertSelective(truck);
    }

    /*修改操作*/
    public Integer updateTruck(Truck truck) {

        return truckMapper.updateByPrimaryKeySelective(truck);

    }


    /*信息删除*/
    public Integer deleteTruckById(Integer id) {
        Truck truck = new Truck();
        truck.setTruckid(id);
        truck.setIsdelete(0);  /*当值为0 就是删除 并执行 返回语句*/

        /*根据 传入的值 进行更新 有值就更新 没值就不更 从而实现删除*/
        return truckMapper.updateByPrimaryKeySelective(truck);
    }


    /*查询分配的车辆信息*/
    public RespPageBean getTruckByPageAndState(Integer page, Integer size, Integer state) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Truck> list = truckMapper.getTruckByPageAndState(page, size, state);
        Long total = truckMapper.getTotalByState(state);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setTotal(total);
        respPageBean.setData(list);
        return respPageBean;
    }
}